package controller;

import pojo.User;
import service.UserService;
import view.SelfUserView;
import view.UserDetails;
import view.UserView;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UserDetailsListener implements ActionListener {
    private UserDetails userDetails;
    private UserView userView;

    public UserDetailsListener(UserDetails dialog, UserView userView){
        super();
        this.userDetails =dialog;
        this.userView=userView;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("返回")) {
            userDetails.dispose();
        }
        if (e.getActionCommand().equals("修改")) {
            UserService userService = new UserService();
            String userId= userDetails.getUser().getUserId();
            String userName= userDetails.getTfName().getText();
            String userGender= userDetails.getTfGender().getText();
            String userPN= userDetails.getTfPN().getText();
            String userType= (String) userDetails.getCBType().getSelectedItem();
            String passWord= userDetails.getUser().getPassWord();
            User user = new User(userId,userName,passWord,userGender,userPN,userType);
            int a = JOptionPane.showConfirmDialog(userDetails, "确认修改?", "确认信息", JOptionPane.YES_NO_OPTION);
            if (a == JOptionPane.YES_OPTION) {
                userService.update(user);
                userDetails.dispose();
                userView.refresh();
            }
        }
        if (e.getActionCommand().equals("删除")) {
            UserService userService = new UserService();
            int a = JOptionPane.showConfirmDialog(userDetails, "确认删除?", "确认信息", JOptionPane.YES_NO_OPTION);
            if (a == JOptionPane.YES_OPTION) {
                userService.delete(userDetails.getUser().getUserId());
                userDetails.dispose();
                userView.refresh();
            }
        }
    }
}
